package com.zll.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zll.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getUser/{id}")
    @HystrixCommand(fallbackMethod = "getUserFallback")
    public User getUser(@PathVariable("id")Long id) {
       //String url = "http://localhost:1020/user/getUser/"+id;
        String url = "http://user-service/user/getUser/"+id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
    // 响应托底数据
    public User getUserFallback(Long id) {
        return new User(-1L,"熔断触发","当前服务不可用，请稍后重试！");
    }
}
